#region Copyright

#endregion
#region Usings

using System;
using System.Globalization;
using System.IO;
using System.Web.Configuration;
using System.Xml;
using System.Xml.XPath;
using TamoCRM.Core.Common.Utilities;
using TamoCRM.Core.Providers;


#endregion

namespace TamoCRM.Core.Common.Utilities
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Config class provides access to the web.config file
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    ///		[cnurse]	11/15/2005	documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public class Config
    {    	
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the default connection String as specified in the provider.
        /// </summary>
        /// <returns>The connection String</returns>
        /// <remarks></remarks>
        /// <history>
        ///		[cnurse]	11/15/2005	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string GetConnectionString()
        {
            return GetConnectionString(GetDefaultProvider("data").Attributes["connectionStringName"]);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the specified connection String
        /// </summary>
        /// <param name="name">Name of Connection String to return</param>
        /// <returns>The connection String</returns>
        /// <remarks></remarks>
        /// <history>
        ///		[cnurse]	11/15/2005	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string GetConnectionString(string name)
        {
            string connectionString = "";
			//First check if connection string is specified in <connectionstrings> (ASP.NET 2.0)
            if (!String.IsNullOrEmpty(name))
            {
                //ASP.NET 2 version connection string (in <connectionstrings>)
                //This will be for new v4.x installs or upgrades from v4.x
                connectionString = WebConfigurationManager.ConnectionStrings[name].ConnectionString;
            }
            if (String.IsNullOrEmpty(connectionString))
            {
                if (!String.IsNullOrEmpty(name))
                {
					//Next check if connection string is specified in <appsettings> (ASP.NET 1.1)
                	//This will accomodate upgrades from v3.x
                    connectionString = GetSetting(name);
                }
            }
            return connectionString;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the specified database owner
        /// </summary>
        /// <returns>The database owner</returns>
        /// <remarks></remarks>
        /// <history>
        ///		[cnurse]	02/13/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string GetDataBaseOwner()
        {
            string databaseOwner = GetDefaultProvider("data").Attributes["databaseOwner"];
            if (!String.IsNullOrEmpty(databaseOwner) && databaseOwner.EndsWith(".") == false)
            {
                databaseOwner += ".";
            }
            return databaseOwner;
        }

        public static Provider GetDefaultProvider(string type)
        {
            ProviderConfiguration providerConfiguration = ProviderConfiguration.GetProviderConfiguration(type);
            //Read the configuration specific information for this provider
            return (Provider)providerConfiguration.Providers[providerConfiguration.DefaultProvider];
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the specified object qualifier
        /// </summary>
        /// <returns>The object qualifier</returns>
        /// <remarks></remarks>
        /// <history>
        ///		[cnurse]	02/13/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string GetObjectQualifer()
        {
            Provider provider = GetDefaultProvider("data");
            string objectQualifier = provider.Attributes["objectQualifier"];
            if (!String.IsNullOrEmpty(objectQualifier) && objectQualifier.EndsWith("_") == false)
            {
                objectQualifier += "_";
            }
            return objectQualifier;
        }


        public static Provider GetProvider(string type, string name)
        {
            ProviderConfiguration providerConfiguration = ProviderConfiguration.GetProviderConfiguration(type);
            //Read the configuration specific information for this provider
            return (Provider)providerConfiguration.Providers[name];
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the specified provider path
        /// </summary>
        /// <returns>The provider path</returns>
        /// <remarks></remarks>
        /// <history>
        ///		[cnurse]	02/13/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string GetProviderPath(string type)
        {
            Provider objProvider = GetDefaultProvider(type);
            string providerPath = objProvider.Attributes["providerPath"];
            return providerPath;
        }

        public static string GetSetting(string setting)
        {
            return WebConfigurationManager.AppSettings[setting];
        }

        public static object GetSection(string section)
        {
            return WebConfigurationManager.GetWebApplicationSection(section);
        }

    }
}